// pages/test_sheet/test_sheet.js
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:"",
    zid:"",
    j_id:"",
    imglist:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var id = options.id;
    app.util.request({
      'url': 'entry/wxapp/hzbingli.url',
      success: function (res) {
        console.log(res)
        that.setData({
          url: res.data,
        })
      }
    })
    that.setData({
      id:id,
      j_id:options.j_id,
      z_id:options.z_id,
    })
    that.getDetail();
  },
  // 上传图片
  bindadd: function () {
    var that = this;
    
    wx.chooseImage({
      count: 9 - that.data.imglist.length, // 默认9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有 'original', 
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        //上传图片
        var successUp = 0; //成功个数
        var failUp = 0; //失败个数
        var length = res.tempFilePaths.length; //总共个数

        var i = 0; //第几个
        that.uploadDIY(res.tempFilePaths, successUp, failUp, i, length);
      },
    });
  },
  uploadDIY(filePaths, successUp, failUp, i, length) {
    var that = this;
    var uniacid = app.siteInfo.uniacid;
    var imglist = that.data.imglist;
    wx.uploadFile({
      url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Uploadback&m=hyb_yl',
      filePath: filePaths[i],
      name: 'upfile',
      formData: {},
      success: (resp) => {
        successUp++;
        imglist.push(resp.data);
        that.setData({
          imglist: imglist
        })
      },
      fail: (res) => {
        failUp++;
      },
      complete: () => {
        i++;
        if (i == length) {
          wx.hideLoading();
        } else { //递归调用uploadDIY函数
          this.uploadDIY(filePaths, successUp, failUp, i, length);

        }
      },
    });
  },
  //图片删除
  binddel(e){
    var t = this, ss = t.data.imglist,i=e.currentTarget.dataset.dex;
    ss.splice(i,1);
    t.setData({
      imglist:ss
    })
  },
  getDetail:function(res)
  {
    var that = this;
    app.util.request({
      'url':"entry/wxapp/follow.suifang_detail",
      data:{
        id:that.data.id
      },
      success:function(res)
      {
        that.setData({
          info:res.data,
          content:res.data.checks
        })
        
      }
    })
  },
  // 请填写
  bindinput(e){
    var that = this;
    var value = e.detail.value
    var index = e.target.dataset.index
    var content = that.data.content;
    content[index].value = value;
    that.setData({
      content:content
    })
    console.log("填写值",value)

  },
  // 完成
  bindsubmit(){
    var that = this;
    var id = that.data.id;
    var imglist = that.data.imglist;
    if(imglist.length == 0)
    {
      wx.showToast({
        title: '请先上传照片',
      })
      return;
    }else{
      app.util.request({
        'url':"entry/wxapp/follow.add_follow",
        data:{
          j_id:that.data.info.j_id,
          imglist:JSON.stringify(imglist),
          content:that.data.content,
          id:that.data.info.id,
          openid:wx.getStorageSync('openid'),
          zid:that.data.info.zid
        },
        success:function(res)
        {
          wx.showToast({
            title: '提交成功',
          })
        }
      })
    }
  },
})